import store from "@/store";
import { appGetStorage } from "@/utils/storage";
export const USER_LOCATION_KEY = "USER_LOCATION";
export const USER_CURRENT_LOCATION_KEY = "USER_CURRENT_LOCATION";
export const getUserLocation = () => {
  return new Promise((onResolve) => {
    const storageLocation = appGetStorage(USER_LOCATION_KEY);
    const storageCurrentLocation = appGetStorage(USER_CURRENT_LOCATION_KEY);
    const location =
      store?.state["sp-common/user"]?.location?.latitude !== null &&
      store?.state["sp-common/user"]?.location?.latitude !== undefined &&
      store?.state["sp-common/user"]?.location?.longitude !== null &&
      store?.state["sp-common/user"]?.location?.longitude !== undefined
        ? store?.state["sp-common/user"]?.location
        : storageLocation &&
          storageLocation?.latitude !== null &&
          storageLocation?.latitude !== undefined &&
          storageLocation?.longitude !== null &&
          storageLocation?.longitude !== undefined
        ? storageLocation
        : null;
    const currentLocation =
      store?.state["sp-address/location"]?.currentLocation?.latitude !== null &&
      store?.state["sp-address/location"]?.currentLocation?.latitude !==
        undefined &&
      store?.state["sp-address/location"]?.currentLocation?.longitude !==
        null &&
      store?.state["sp-address/location"]?.currentLocation?.longitude !==
        undefined
        ? store?.state["sp-address/location"]?.currentLocation
        : storageCurrentLocation &&
          storageCurrentLocation?.latitude !== null &&
          storageCurrentLocation?.latitude !== undefined &&
          storageCurrentLocation?.longitude !== null &&
          storageCurrentLocation?.longitude !== undefined
        ? storageCurrentLocation
        : null;

    if (location) {
      const params = {
        location,
        currentLocation,
      };
      console.log("调用$getUserLocation-含有参数", params);
      onResolve(params);
    } else {
      console.log("调用$getUserLocation-不含参数", params);
      const params = {
        location: {},
        currentLocation,
      };
      uni.authorize({
        scope: "scope.userLocation",
        success: () => {
          uni.getLocation({
            isHighAccuracy: true,
            type: "gcj02",
            success: (res) => {
              params.location = res;
              store?.dispatch("sp-common/user/setUserLocation", res);
              onResolve(params);
            },
            fail: () => {
              onResolve(params);
              // onRejected(err);
            },
          });
        },
        fail: () => {
          onResolve(params);
          // onRejected(err);
        },
      });
    }
  });
};

export const getLocation = () => {
  const storageLocation = appGetStorage(USER_LOCATION_KEY);
  const location =
    store?.state["sp-common/user"]?.location?.latitude !== null &&
    store?.state["sp-common/user"]?.location?.latitude !== undefined &&
    store?.state["sp-common/user"]?.location?.longitude !== null &&
    store?.state["sp-common/user"]?.location?.longitude !== undefined
      ? store?.state["sp-common/user"]?.location
      : storageLocation &&
        storageLocation?.latitude !== null &&
        storageLocation?.latitude !== undefined &&
        storageLocation?.longitude !== null &&
        storageLocation?.longitude !== undefined
      ? storageLocation
      : null;
  return location;
};
export const getCurrentLocation = () => {
  const storageCurrentLocation = appGetStorage(USER_CURRENT_LOCATION_KEY);
  const currentLocation =
    store?.state["sp-address/location"]?.currentLocation?.latitude !== null &&
    store?.state["sp-address/location"]?.currentLocation?.latitude !==
      undefined &&
    store?.state["sp-address/location"]?.currentLocation?.longitude !== null &&
    store?.state["sp-address/location"]?.currentLocation?.longitude !==
      undefined
      ? store?.state["sp-address/location"]?.currentLocation
      : storageCurrentLocation &&
        storageCurrentLocation?.latitude !== null &&
        storageCurrentLocation?.latitude !== undefined &&
        storageCurrentLocation?.longitude !== null &&
        storageCurrentLocation?.longitude !== undefined
      ? storageCurrentLocation
      : null;
  return currentLocation;
};
